package com.me.safebox.ui.curl;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;

public class ViewUtil {
	/**
	 * added by yangguangfu Draw the view into a bitmap.
	 */
	public Bitmap getViewBitmap(View v) {
		Bitmap bitmap = null;
		if (v != null) {
			v.clearFocus();
			v.setPressed(false);

			boolean willNotCache = v.willNotCacheDrawing();
			v.setWillNotCacheDrawing(false);

			// Reset the drawing cache background color to fully transparent
			// for the duration of this operation
			int color = v.getDrawingCacheBackgroundColor();
			v.setDrawingCacheBackgroundColor(0);
//			float alpha = v.getAlpha();
//			v.setAlpha(1.0f);

			if (color != 0) {
				v.destroyDrawingCache();
			}
			v.buildDrawingCache();
			Bitmap cacheBitmap = v.getDrawingCache();
			if (cacheBitmap == null) {
				Log.e("failed:", "failed getViewBitmap(" + v + ")",
						new RuntimeException());
				return null;
			}

			bitmap = Bitmap.createBitmap(cacheBitmap);

			// Restore the view
			v.destroyDrawingCache();
//			v.setAlpha(alpha);
			v.setWillNotCacheDrawing(willNotCache);
			v.setDrawingCacheBackgroundColor(color);

		}

		return bitmap;
	}
}
